<template>
  <div class="permission">
    <el-card>
      <div style="text-align:right">
        <el-button type="primary" @click="add('0',1)">添加菜单</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name">
          <template v-slot="{ row }">
            <span>
              <i class="el-icon-folder-opened"></i>
              {{ row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="权限标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div>
              
              <el-button type="text" @click="add(row.id,2)">
                添加权限点</el-button
              >
              <el-button type="text" @click="add(row.id,1)">添加页面</el-button>
              <el-button type="text" @click="edit(row)"> 修改</el-button>
              <el-button type="text" @click="del(row)"> 删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增权限 -->
    <Dialog v-model="show" ref="dialog" @getData="getData"> </Dialog>
  
  
  </div>
</template>

<script>
import { sysPermission, sysPermissionDel } from '@/api/permission'
import { changeTreeData } from '@/utils/index.js'
import Dialog from './dialog.vue'

export default {
  components: {
    Dialog,
   
  },
  data () {
    return {
      list: [],
      show: false,
      
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysPermission()
      this.list = changeTreeData(res.data, '0')
      console.log('获取所有权限点', res)
    },
    //删除
    async del (row) {
      this.$confirm(`确定删除${row.name}---数据吗？`, '提示')
        .then(async () => {
          await sysPermissionDel(row.id)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    },

    //添加
    add (pid,type) {
      this.show = true
      //点击项的pid与type闯入弹框组件
      this.$refs.dialog.form.type = type
      this.$refs.dialog.form.pid = pid
    },
    //编辑
    edit(row){
       this.show = true
       //数据回显
       this.$refs.dialog.form=JSON.parse(JSON.stringify(row))
       this.$refs.dialog.mode='edit'
    },
    
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 15px;
}
</style>
